﻿using System;
using System.Collections.Generic;

using E.Model;
using E.Enum;
using E.BLL;
using E.Web.UI.Page;

using X.Common;

namespace E.Web.training.internals.exam.course
{
    public partial class T : BasePage
    {
        public int examid;
        public bool err = false;
        public int categoryid;

        protected override void ShowPage()
        {
            string reffer = XRequest.GetUrlReferrer();
            examid = XRequest.GetInt("examid", -1);
            categoryid = XRequest.GetInt("cid", -1);
            //examid = 1;


            if (examid != -1 && categoryid != -1)
            {
                ExamBLL bll = new ExamBLL();
                int testid = bll.GetOriginalTestID(userid, examid);

                IList<ShortExamSettingModel> examSetting = bll.GetExamShortSetting(examid);

                if (examSetting.Count > 0)
                {
                    QuestionBLL qbll = new QuestionBLL();
                    IList<int> allList = new List<int>();

                    foreach (ShortExamSettingModel settingModel in examSetting)
                    {
                        IList<int> tmplist = qbll.GetQuestionIDByCategory(settingModel.QuestionCategoryID, areaid, settingModel.CourseID, 0);
                        IList<int> tmplist_re = qbll.GetQuestionIDByCategory(settingModel.QuestionCategoryID, areaid, settingModel.CourseID, 1);
                        IList<int> idList = GetRandomValue(settingModel.QuestionQuantity, tmplist);
                        IList<int> idList_re = GetRandomValue(settingModel.RequiredQuestionQuantity, tmplist_re);
                        foreach (int id in idList)
                        {
                            allList.Add(id);
                        }
                        foreach (int id in idList_re)
                        {
                            allList.Add(id);
                        }

                        DateTime now = DateTime.Now;

                        TestModel testModel = new TestModel();
                        testModel.ID = testid;
                        testModel.UserID = userid;
                        testModel.ExamID = examid;
                        testModel.QuestionIDArray = allList;
                        testModel.CreatedTime = now;

                        bll.UpdateUserSecTest(testModel);
                    }
                }
            }
            else
            {
                err = true;
                ExecuteJS("alert('服务器忙，请稍后再试。');");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
